<template>
  <div >
    <form-head @onSubmit="onSubmit" :is_change="is_change"/>
    <el-form ref="form" :rules="rules" :model="form" :label-width="isMobile()?'100px':'200px'" class="layout-all form-width-50">
      <el-form-item label="快递100授权码key:" prop="key">
        <el-input v-model="form.key"  placeholder="请输入快递100授权码key" />
      </el-form-item>
      <el-form-item label="快递100 secret:" prop="secret">
        <el-input v-model="form.secret"  placeholder="请输入快递100 secret" />
      </el-form-item>
      <el-form-item label="打印机siid:" prop="siid">
        <el-input v-model="form.siid"  placeholder="请输入打印机siid" />
      </el-form-item>
      <el-form-item label="打印模版:" prop="tempid">
        <el-input v-model="form.tempid"  placeholder="请输入打印模版(快递100内查找)" />
      </el-form-item>

      <el-form-item label="快递公司:" prop="kuaidicom">
        <el-select
            v-model="form.kuaidicom"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in send_options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
        </el-select>
      </el-form-item>
     <el-form-item label="寄件人姓名:" prop="name" >
        <el-input v-model="form.name" placeholder="请输入寄件人姓名" />
      </el-form-item>
      <el-form-item label="寄件人电话:" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入寄件人电话" />
      </el-form-item>
      <el-form-item label="寄件人地址:" prop="print_addr">
        <el-input v-model="form.print_addr"  placeholder="请输入寄件人地址" />
      </el-form-item>
      <el-form-item label="寄件人公司:" prop="company">
        <el-input v-model="form.company"  placeholder="请输入寄件人公司" />
      </el-form-item>
      <el-form-item label="物流公司授权:" prop="print_addr" >
      <el-card class="box-card" >
      <div slot="header" class="clearfix" >
        <span>物流公司授权</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="onSubmit('form')">去菜鸟授权</el-button>
      </div>
      <el-form-item label="物流公司授权parterId:" prop="partner_id" label-width="200px">
        <el-input v-model="form.partner_id" style="width:100%" placeholder="请输入物流公司授权parterId" />
      </el-form-item>
      <el-form-item label="物流公司授权partnerKey:" prop="partner_key" label-width="200px">
        <el-input v-model="form.partner_key" style="width:100%" placeholder="请输入物流公司授权partnerKey" />
      </el-form-item>
      <el-form-item label="物流公司授权net:" prop="net" label-width="200px">
        <el-input v-model="form.net" style="width:100%" placeholder="请输入物流公司授权net" />
      </el-form-item>
      <el-tag>点击去授权后会提示登录菜鸟账号,并授权</el-tag>
      </el-card>
      </el-form-item>

      <el-form-item label="是否启用:" prop="status">
        <el-switch
          v-model="form.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name:'order_print_form',
  data() {
    return {
      id: 0,
      is_change: undefined,
      show_content: true,
      send_options: [],
      goods_list: [],
      banner: '',
      thumb: '',
      form: {status:true},
      rules: {
        key: [
          { required: true, message: '请输入快递100授权码key', trigger: 'blur' }
        ],
        secret: [
          { required: true, message: '请输入快递100 secret', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入寄件人电话', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入寄件人姓名', trigger: 'blur' }
        ],
        print_addr: [
          { required: true, message: '请输入寄件人地址', trigger: 'blur' }
        ],
        kuaidicom: [
          { required: true, message: '请选择快递公司', trigger: 'blur' }
        ],
      }
    }
  },
  mounted() {
    this.id = (this.$route.params && this.$route.params.id)||0
    this.$setTagsViewTitle(this.id)
    this.get_express_code()
    this.init_into()
  },
  methods: {
    // 获取快递公司编码
    get_express_code() {
      this.get('/shop/OrderGoods/express_code', name, 'POST').then(e => {
        this.send_options = e.list
      })
    },
    // 提交
    onSubmit(formName) {
      const that=this
      const form = this.form
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.status === true ? form.status = 1 : form.status = 0
          this.get('/shop/OrderPrint/save', form, 'POST').then(e => {
            this.id=e.id
            this.form = e.info.tpl
            this.form.status=this.form.status ==1
            if(form.partner_id==''){
              this.get('/shop/OrderPrint/auth', this.form, 'POST').then(e => {
                   window.open(e.data.url,'_blank')
                   that.$msg_confirm_show("是否完成授权?","菜鸟授权").then(e=>{
                     that.init_into()
                   })
              })
            }else{
              that.bk()
            }
          })
          //this.$router.back()
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$back()
    },
    // 获取当前数据
    init_into() {
      this.get('/shop/OrderPrint/info?id=' + this.id, 'POST').then(e => {
        this.form = e.info.tpl
        this.form.status=this.form.status ==1||this.form.status ==''
        setTimeout(()=>{this.is_change=false},100)
      })
    }
  },
  watch: {
    form:{
      handler (val, oldVal) {
        if(this.is_change!=undefined)
          this.is_change=true
      },
      deep:true
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
.menu_title {
  font-size:20px;font-weight:bold;
  padding:10px 0px;
}
</style>
